package descentFly.weaponts
{
	import SEApp.se_util;
	
	import UIFree.uif_Timer;
	
	import UIUtils.uif_util;
	
	import away3d.entities.Sprite3D;
	import away3d.vf.shapes.vf_star;
	
	import descentFly.away3D4.net.df_netChip;
	import descentFly.chips.df_chip;
	import descentFly.chips.df_weapont;
	import descentFly.df_utils;
	import descentFly.weaponts.element.df_plasmaElement;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	import flash.utils.Timer;
	
	import space.sp_utils;
	
	public class df_pulseWeapont extends df_awayWeapont {
		public static const FIELD_PULSEPERIOD: String 	= 'pulsePeriod';
		public static const FIELD_CANNONCOUNT: String 	= 'cannonCount';
		public static const FIELD_OFFSET: String 		= 'offset';
		
		public static const DEFAULT_CANNONCOUNT: Number = 2;
		public static const DEFAULT_PULSEPERIOD: Number = 100;
		public static const DEFAULT_OFFSET: Array = [
			{x:-10, y:0, z:0},
			{x:10, y:0, z:0}
		]
		
		private var _pulseTimer		: Timer;
		private var _lIndex			: int;
		
		public function df_pulseWeapont(a_chip: df_chip, a_options: Object) {
			a_options = sp_utils.checkParam(a_options, FIELD_PULSEPERIOD, DEFAULT_PULSEPERIOD);
			a_options = sp_utils.checkParam(a_options, FIELD_CANNONCOUNT, DEFAULT_CANNONCOUNT);
			a_options = sp_utils.checkParam(a_options, FIELD_OFFSET, DEFAULT_OFFSET);
			super(a_chip, a_options);
			_pulseTimer = new Timer(options.pulsePeriod)
			_pulseTimer.addEventListener(TimerEvent.TIMER, doPulse);
			_lIndex		= 0;
		}
		
		override protected function removeEvents():void {
			_pulseTimer.removeEventListener(TimerEvent.TIMER, doPulse);
		}
		
		override public function dispose():void {
			_pulseTimer.stop();
			super.dispose();
		}
		
		override protected function checkLimit():Boolean {
			return (!limited || (ammo > 0)) || (chip is df_netChip);
		}
		
		override protected function setFire(a_fire: Boolean): void {
			if (a_fire) {
				doPulse();
				_pulseTimer.start();
			} else _pulseTimer.stop();
		}
		
		override public function get isFire():Boolean {
			return _pulseTimer.running;
		}
		
		protected function doPulse(e: Event=null): void {
			if (!Space.disableScene) {
				_lIndex++;
				var bullet: df_pulseElement = new (options.bullet.Class)(chip, options.bullet.options);
				
				var cannonIndex: int = _lIndex % options.cannonCount;
				var offset: Vector3D = new Vector3D(options.offset[cannonIndex].x, options.offset[cannonIndex].y, options.offset[cannonIndex].z);
				var t: Matrix3D = chipAway.body.skin.transform.clone();
				t.prependTranslation(offset.x, offset.y, offset.z);
	//			t.prependTranslation(((_lIndex % 2) * 20 - 10), 0, 0);
				bullet.start(t);
				scene.addChild(bullet);
				if (limited) ammo -= 1;
			}
		}
	}
}